<!-- 司机选择组件 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openAdd = true"
        >添加司机</el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-table v-loading="loading" :data="driverList">
          <el-table-column label="姓名" align="center" prop="driverName" />
          <el-table-column label="手机号" align="center" prop="telephone" width="115" />
          <el-table-column label="准驾车型" align="center" prop="vehicleClass" />
          <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
          <el-table-column label="认证状态" align="center" prop="isCert" :formatter="isCertFormat" />
          <el-table-column label="状态" align="center" width="140" >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="2"
                active-text="正常"
                inactive-text="禁用" disabled>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="select(scope.row)"
              >选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.current"
          :limit.sync="queryParams.size"
          @pagination="getList"
        />
      </el-col>
      <el-col :span="2">
        <el-button style="margin: 0 30%;" type="primary" icon="el-icon-arrow-right" plain circle></el-button>
      </el-col>
      <el-col :span="10">
        <el-table :data="driverListSelect">
          <el-table-column label="姓名" align="center" prop="driverName" />
          <el-table-column label="手机号" align="center" prop="telephone" />
          <el-table-column label="准驾车型" align="center" prop="vehicleClass" />
          <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="scope">
              <el-button
                style="color: red;"
                size="mini"
                type="text"
                @click="remove(scope.row)"
              >移除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <div class="ds-f">
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>

    <!-- 物流公司添加司机对话框 -->
    <el-dialog title="添加司机" :visible.sync="openAdd" width="300px" append-to-body :close-on-click-modal="false">
      <driver-add @driverAdd="driverAdd" />
    </el-dialog>

  </div>
</template>

<script>
import { listDriver, listDriverAll } from "@/api/tws/driver";
import DriverAdd from '@/components/DriverAdd/index';

export default {
  name: "DriverSelect",
  components: {
    DriverAdd
  },
  props: {
    // 已选择司机
    list: {
      type: Array
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      remoteLoading: false,
      // 是否显示弹出层
      openAdd: false,
      userType: '',
      driver: '',
      // 总条数
      total: 0,
      // 认证状态字典
      isCertOptions: [],
      // 性别字典
      sexOptions: [],
      // 司机信息表格数据
      driverList: [],
      driverListSelect: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        driverName: null,
        drivingLicense: null,
        telephone: null,
        qualificationCertificate: null,
        vehicleClass: null,
        issuingOrganizations: null,
        validPeriodFrom: null,
        validPeriodTo: null,
        code: null,
        sex: null,
        age: null,
        isCert: null,
        state: null,
      }
    };
  },
  watch: {
    list: function(newData, oldData) {
      if(newData) {
        this.driverListSelect = newData;
      }
    }
  },
  created() {
    this.userType = this.$store.state.user.type;
    this.getDict();
    this.getList();
  },
  methods: {
    /** 查询字典 */
    getDict() {
      // 认证状态字典
      this.getDicts("is_cert").then(response => {
        this.isCertOptions = response.data;
      });
      // 性别字典
      this.getDicts("sys_user_sex").then(response => {
        this.sexOptions = response.data;
      });
    },
    /** 查询司机信息列表 */
    getList() {
      this.loading = true;
      // 物流公司登陆只查常用司机
      if(this.userType == this.$store.state.constants.userType_company) {
        listDriver(this.queryParams).then(response => {
          this.driverList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        listDriverAll(this.queryParams).then(response => {
          this.driverList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 认证状态字典翻译
    isCertFormat(row, column) {
      return this.selectDictLabel(this.isCertOptions, row.isCert);
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 添加司机的回调
    driverAdd(query) {
      this.getList();
      this.openAdd = false;
    },
    // 选择司机
    select(row) {
      var index = this.driverListSelect.indexOf(row);
      if (index !== -1) {
        this.$modal.msgError("已选择");
      } else {
        this.driverListSelect.push(row);
      }
    },
    // 移除司机
    remove(row) {
      var index = this.driverListSelect.indexOf(row);
      if (index !== -1) {
        this.driverListSelect.splice(index, 1);
      }
    },
    submitForm() {
      this.$emit("driverSelect", this.driverListSelect);
    }
  }
};
</script>

<style scoped>
.ds-f {
  text-align: center;
  margin-top: 40px;
}
</style>
